package com.yxq.mdt.app.widgets.pagemenulibrary;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import com.yxq.mdt.app.R;
import com.yxq.mdt.app.widgets.pagemenulibrary.holder.ScreenUtil;


/**
 * Author: Mr.xiao on 2017/5/23
 *
 * @mail:xhb_199409@163.com
 * @github:https://github.com/xiaohaibin
 * @describe: 指示器
 */
public class IndicatorView extends LinearLayout {

    private int indicatorColor = Color.GRAY;
    private int indicatorColorSelected = Color.RED;
    private int indicatorWidth = 6;
    private int gravity = 0;
    private Paint mPaint;
    private int indicatorCount = 3;
    private int currentIndicator = 0;
    private boolean isMenu = false;

    public void setMenu(boolean menu) {
        isMenu = menu;
    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0x12) {
                invalidate();
            }
        }
    };

    public IndicatorView(Context context) {
        this(context, null, 0);
    }

    public IndicatorView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public IndicatorView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
        if (attrs != null) {
            TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.IndicatorView);
            indicatorColor = typedArray.getColor(R.styleable.IndicatorView_indicatorColor, Color.rgb(0, 0, 0));
            indicatorColorSelected = typedArray.getColor(R.styleable.IndicatorView_indicatorColorSelected, Color.rgb(0, 0, 0));
            indicatorWidth = ScreenUtil.dip2px(typedArray.getInt(R.styleable.IndicatorView_indicatorWidth, 0));
            gravity = typedArray.getInt(R.styleable.IndicatorView_gravity, 0);
            typedArray.recycle();
        }
    }


    private void init() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        indicatorColor = Color.rgb(0, 0, 0);
        indicatorColorSelected = Color.rgb(0, 0, 0);
    }

    private void indicator() {
        removeAllViews();
        for (int i = 0; i < indicatorCount; i++) {
            ImageView pointView = new ImageView(getContext());
            pointView.setScaleType(ImageView.ScaleType.CENTER);
            pointView.setPadding(5, 0, 5, 0);
            pointView.setPadding(indicatorWidth, indicatorWidth, indicatorWidth, indicatorWidth);

            LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            layoutParams.setMargins(10, 0, 10, 0);//4个参数按顺序分别是左上右下
            pointView.setLayoutParams(layoutParams);

            if (isMenu) {
                if (i == currentIndicator) {
                    pointView.setImageResource(R.mipmap.yellow_circle);
                } else {
                    pointView.setImageResource(R.mipmap.lin_circle);
                }
            } else {
                if (i == currentIndicator) {
                    pointView.setImageResource(R.mipmap.yellow_circle);
                } else {
                    pointView.setImageResource(R.mipmap.lin_circle);
                }
            }

            addView(pointView);
        }
    }

    public void setIndicatorCount(int indicatorCount) {
        this.indicatorCount = indicatorCount;
        indicator();
    }

    public void setCurrentIndicator(int currentIndicator) {
        this.currentIndicator = currentIndicator;
        indicator();
    }

}